package de.roskenet.clou.test;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbcp.BasicDataSource;


public class PageImport {
	
	private static final String SQL_SELECT_PAGE =
			"select * FROM clouelement WHERE name=?";
	
	private BasicDataSource dataSource;
	public BasicDataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(BasicDataSource dataSource) {
		this.dataSource = dataSource;
	}

	public byte[] getPage(String pageName) {
		byte[] pageElement = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			conn = dataSource.getConnection();
			stmt = conn.prepareStatement(SQL_SELECT_PAGE);
			stmt.setString(1, pageName);
			rs = stmt.executeQuery();
			
			if(rs.next()) {
				pageElement = rs.getBytes("content");
				System.out.println("OUTPUT: ");
				System.out.println(pageElement);
			}
			// Wir hoffen, dass alles gutgegangen ist! :-)
			rs.close();
			stmt.close();
			conn.close();
			
		} catch (SQLException sqle) {
			System.out.println("Something went wrong!");
		}
		
		return pageElement;
	}
	
	public String getMimeType(String pageName) {
		String mimetype = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			conn = dataSource.getConnection();
			stmt = conn.prepareStatement(SQL_SELECT_PAGE);
			stmt.setString(1, pageName);
			rs = stmt.executeQuery();
			
			if(rs.next()) {
				mimetype = rs.getString("mimetype");
			}
			// Wir hoffen, dass alles gutgegangen ist! :-)
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException sqle) {
			System.out.println("Something went wrong!");
		}
		
		return mimetype;
	}
	
	public List<String> getAllPageNames() {
		List<String> theList = new ArrayList();
		
		return theList;
	}
}
